package com.carkeeper.order.service.impl;

import com.carkeeper.entity.PageResult;
import com.carkeeper.entity.Result;
import com.carkeeper.order.dao.SerOrderDetailMapper;
import com.carkeeper.order.dao.SerOrderLogMapper;
import com.carkeeper.order.dao.SerOrderMapper;
import com.carkeeper.order.pojo.PostSerOrder;
import com.carkeeper.order.pojo.SerOrder;
import com.carkeeper.order.pojo.SerOrderDetail;
import com.carkeeper.order.pojo.SerOrderLog;
import com.carkeeper.order.service.SerOrderService;
import com.carkeeper.subject.feign.StoreFeign;
import com.carkeeper.subject.pojo.StoreFull;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class SerOrderServiceImpl implements SerOrderService {

    @Autowired
    private SerOrderMapper serOrderMapper;

    @Autowired
    private SerOrderDetailMapper serOrderDetailMapper;

    @Autowired
    private SerOrderLogMapper serOrderLogMapper;

    @Autowired
    private StoreFeign storeFeign;

    /**
     * 分页查询所有服务订单
     * @return
     */
    @Override
    public PageInfo<SerOrder> pageSerOrder(Integer currentpage, Integer pagesize) {
        //设置默认
        int cuPage=1;//页码
        int pageSi=10;//每页条数

        //判空
        if (currentpage != null && currentpage > 0) {
            cuPage = currentpage;
        }
        if (pagesize != null && pagesize > 0) {
            pageSi = pagesize;
        }
        //分页
        PageHelper.startPage(cuPage, pageSi);
        List<SerOrder> serOrders = serOrderMapper.selectAll();//查询所有
        PageInfo<SerOrder> pageInfo = new PageInfo<>(serOrders);
        return pageInfo;
    }

    /**
     * 多条件查询
     * @param map
     * @return
     */
    @Override
    public PageInfo<SerOrder> SearchSerOrder(Map map) {
        //判空
        if (map == null || map.size() == 0) {
            PageInfo<SerOrder> serOrderPageInfo = pageSerOrder(1, 10);
            return serOrderPageInfo;
        }
        PageHelper.startPage(1, 10);
        Example example = createExample(map);//自定义方法
        List<SerOrder> serOrders = serOrderMapper.selectByExample(example);
        return new PageInfo<>(serOrders);
    }

    /**
     * 查询订单详情
     * @param serOrderId
     * @return
     */
    @Override
    public PostSerOrder findPostSerOrderById(BigInteger serOrderId) {
        PostSerOrder postSerOrder = new PostSerOrder();

        //封装订货单信息
        SerOrder serOrder = serOrderMapper.selectByPrimaryKey(serOrderId);
        if (serOrder == null) {
            return null;
        }
        postSerOrder.setSerOrder(serOrder);

        //封装服务项目
        SerOrderDetail serOrderDetail = new SerOrderDetail();
        serOrderDetail.setOrderId(serOrderId);
        List<SerOrderDetail> serOrderDetailList = serOrderDetailMapper.select(serOrderDetail);
        if (serOrderDetailList == null||serOrderDetailList.size()<=0) {
            return null;
        }
        postSerOrder.setSerOrderDetailList(serOrderDetailList);

        //操作记录
        SerOrderLog serOrderLog = new SerOrderLog();
        serOrderLog.setOrderId(serOrderId);
        List<SerOrderLog> serOrderLogList = serOrderLogMapper.select(serOrderLog);
        if (serOrderLogList == null||serOrderLogList.size()<=0) {
            return null;
        }
        postSerOrder.setSerOrderLogList(serOrderLogList);

        //商家信息
        Result<StoreFull> result = storeFeign.findById(serOrder.getStoreId().toString());
        StoreFull storeFull = result.getData();
        postSerOrder.setStoreFull(storeFull);
        return postSerOrder;
    }

    /**
     * 更改订单状态
     * @param
     */
    @Transactional
    @Override
    public int updateStatus(SerOrder serOrder) {
        //查询条件
        Example example=new Example(SerOrder.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", serOrder.getId());
        /*
            更改
            serOrder里封装要修改的数据
            example表示判断条件
         */
        int i = serOrderMapper.updateByExampleSelective(serOrder, example);
        return i;
    }

    /**
     * 过期订单状态更改
     */
    @Transactional
    @Override
    public int overTimeUpdate(SerOrder serOrder) {
        SerOrder serOrder1 = serOrderMapper.selectByPrimaryKey(serOrder.getId());
        String orStatus = serOrder1.getOrderStatus().toString();//获取当前订单状态
        if ("1".equals(orStatus)) {
            //要更改的数据
            serOrder.setOrderStatus('0');
            //查询条件
            Example example=new Example(SerOrder.class);
            Example.Criteria criteria = example.createCriteria();
            criteria.andEqualTo("id", serOrder.getId());
            //更改
            int i = serOrderMapper.updateByExampleSelective(serOrder, example);
            return i;
        }
        return 1;
    }

    /**
     * 分页查询未处理服务订单
     * @param currentpage
     * @param pagesize
     * @return
     */
    @Override
    public PageResult<SerOrder> UntreatPageSerOrder(Integer currentpage, Integer pagesize) {

        //默认页码，每页条数
        int cuPage=1;
        int pageSi=10;

        if (currentpage != null && currentpage > 0) {
            cuPage = currentpage;
        }
        if (pagesize != null && pagesize > 0) {
            pageSi = pagesize;
        }
        PageHelper.startPage(cuPage, pageSi);

        Example example=new Example(SerOrder.class);
        Example.Criteria criteria = example.createCriteria();
        List<Character> list = new ArrayList();
        list.add('1');
        list.add('2');
        criteria.andIn("orderStatus", list);//查询订单状态为待处理1，过期订单0
        List<SerOrder> serOrders = serOrderMapper.selectByExample(example);

        PageInfo<SerOrder> pageInfo = new PageInfo<>(serOrders);

        return new PageResult<>(pageInfo.getTotal(), pageInfo.getList());
    }

    /**
     * 构建查询对象
     * @param
     * @return
     */
    private Example createExample(Map map){
        Example example=new Example(SerOrder.class);
        Example.Criteria criteria = example.createCriteria();
        if(map.get("code")!=null && !"".equals(map.get("code"))){
            criteria.andEqualTo("code", map.get("code"));//订单编号
        }
        if(map.get("contact")!=null && !"".equals(map.get("contact"))){
            criteria.andEqualTo("contact", map.get("contact"));//客户名称
        }
        if(map.get("storeName")!=null && !"".equals(map.get("storeName"))){
            criteria.andEqualTo("storeName", map.get("storeName"));//商家名称
        }
        if(map.get("createDate")!=null && !"".equals(map.get("createDate"))){
            criteria.andEqualTo("createDate", map.get("createDate"));//订单时间
        }
        if(map.get("payStatus")!=null && !"".equals(map.get("payStatus"))){
            criteria.andEqualTo("payStatus", map.get("payStatus"));//支付状态
        }
        if(map.get("orderStatus")!=null && !"".equals(map.get("orderStatus"))){
            criteria.andEqualTo("orderStatus", map.get("orderStatus"));//订单状态
        }
        return example;
    }
}
